package com.wbao.dianniu.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.AnonymousAdapter;
import com.wbao.dianniu.data.QaResponse;
import com.wbao.dianniu.listener.IQaListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.update.AmDataChangeManager;
import com.wbao.dianniu.update.AmDataChangeManager.IAmDataListener;
import com.wbao.dianniu.manager.QaManager;
import com.wbao.dianniu.utils.Utils;

import java.util.List;

/**
 * 我发的匿名
 *
 * @author yejianjun
 */
public class SendAmFragment extends Fragment implements IQaListener, OnClickListener, IAmDataListener {

    private LinearLayout noDataLayout;
    private Button sendBtn;
    private PullToRefreshListView pullListView;
    private ListView mListView;
    private QaManager manager;
    private int TYPE = 4;
    private final int PAGE_COUNTS = 10;
    private AnonymousAdapter adapter;
    private List<QaResponse> mList;
    private AmDataChangeManager amDataChangeManager;

    public static SendAmFragment instance(Context context) {
        SendAmFragment view = new SendAmFragment();
        return view;
    }

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_send_qa, null);
        adapter = new AnonymousAdapter(view.getContext(), 1);
        amDataChangeManager = AmDataChangeManager.getInstance();
        amDataChangeManager.addAmChangeListener(this);
        initView(view);
        requestData();
        return view;
    }

    private void requestData() {
        manager = new QaManager(getActivity());
        manager.addQaListener(this);
        reqData("down");
    }

    private void initView(View view) {
        noDataLayout = (LinearLayout) view.findViewById(R.id.no_data_layout);
        sendBtn = (Button) view.findViewById(R.id.send_qa_button);
        sendBtn.setText(getResources().getString(R.string.ask_fist_question));
        pullListView = (PullToRefreshListView) view.findViewById(R.id.list_send_qa);
        pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
        pullListView.setOnRefreshListener(onRefreshListener);
        mListView = pullListView.getRefreshableView();
        registerForContextMenu(mListView);
        mListView.setAdapter(adapter);
        sendBtn.setOnClickListener(this);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                Utils.intoAmDetailActivity(getContext(), (QaResponse) adapter.getItem(position - 1));
            }
        });
    }

    OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            // 上拉刷新 加载请求数据
            pullUpRequest();
        }
    };

    // 上拉刷新数据请求
    private void pullUpRequest() {
        // 没有搜索结果 或者数据为空 上拉没有效果
        reqData("up");
    }

    private void reqData(String refresh) {
        manager.reqData(GlobalContext.getAccountId(), null, TYPE, 0, PAGE_COUNTS, adapter.getLastId(), refresh);
    }

    @Override
    public void onQaFailure(int errorCode, String errorMsg) {
        pullListView.onRefreshComplete(false, false);
    }

    @Override
    public void onQaSuccess(List<QaResponse> list) {
        pullListView.onRefreshComplete(false, false);
        if (null != list) {
            adapter.addBackwardList(list);
            mList = adapter.getList();
            adapter.notifyDataSetChanged();
            if (mList.size() == 0) {
                noDataLayout.setVisibility(View.VISIBLE);
            } else {
                noDataLayout.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(null != amDataChangeManager)
            amDataChangeManager.removeAmChangeListener(this);
        if (null != manager) {
            manager.removeQaListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_qa_button:
                Intent intent = new Intent();
                intent.setClass(getActivity(), AmQuestionActivity.class);
                getActivity().startActivity(intent);
                break;

            default:
                break;
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SendAnonyFragment");
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SendAnonyFragment");
    }

    @Override
    public void addOneData(QaResponse addData) {
        // TODO Auto-generated method stub
        noDataLayout.setVisibility(View.GONE);
        adapter.addOneQuestData(addData);
    }

    @Override
    public void removeOneData(int questId) {
        // TODO Auto-generated method stub
        adapter.removeOneData(questId);
        mList = adapter.getList();
        if (0 == mList.size()) {
            noDataLayout.setVisibility(View.VISIBLE);
        } else {
            noDataLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void priseNotify(int questId, int isGood, int goodCounts) {
        // TODO Auto-generated method stub
        adapter.priseNotify(questId, isGood, goodCounts);
    }

    @Override
    public void replyNotify(int questId, int answerCounts) {
        // TODO Auto-generated method stub
        adapter.replyNotify(questId, answerCounts);
    }

    @Override
    public void updateVisitor(int questId, int visitorCounts) {
        adapter.updateVisitor(questId,visitorCounts);
    }
}
